﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Xeml.Document.Settings
{
    public class DataProviderSettings: ConfigurationSection
    {
        private static DataProviderSettings providers = ConfigurationManager.GetSection("DataProviders") as DataProviderSettings;

        public static DataProviderSettings DataProviders {
            get { return providers; }
        }

    }

    [ConfigurationCollection(typeof(DataProviderElement))]
    public class DataProviderElementCollection : ConfigurationElementCollection
    {
        public DataProviderElement this[int index]
        {
            get { return BaseGet(index) as DataProviderElement; }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DataProviderElement)(element)).DataProviderUri;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new DataProviderElement();
        }
    }

    public class DataProviderElement : ConfigurationElement
    {

        public DataProviderElement() { }

        #region name-value pairs
        [ConfigurationProperty("username", IsRequired=true)]
        public string Username
        {
            get { return (String)this["username"]; }
            set { this["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired=true)]
        public string Password
        {
            get { return (String)this["password"]; }
            set { this["password"] = value; }
        }

        [ConfigurationProperty("server", IsRequired=true)]
        public string Server 
        {
            get { return (String)this["server"]; }
            set { this["server"] = value; }
        }

        [ConfigurationProperty("database", IsRequired=true)]
        public string Database
        {
            get { return (String)this["database"]; }
            set { this["database"] = value; }
        }

        [ConfigurationProperty("dataProviderUri", IsRequired=true, IsKey=true)]
        public string DataProviderUri
        {
            get { return (String)this["dataProviderUri"]; }
            set { this["dataProviderUri"] = value; }
        }
        #endregion
    }
}
